package com.dq.controller.api.small;

import com.dq.exception.customException.AdminException;
import com.dq.vo.Vo;
import com.dq.domain.base.AbstractEntity;
import com.dq.domain.orders.UserOrder;
import com.dq.domain.record.MaintainRecord;
import com.dq.domain.org.Org;
import com.dq.domain.record.PayrollRecord;
import com.dq.domain.record.ServiceRecord;
import com.dq.domain.reward.Reward;
import com.dq.domain.setup.WorkRoyaltySet;
import com.dq.domain.sys.user.user.StaffUser;
import com.dq.domain.sys.user.user.base.SysUser;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.service.AllService;
import com.dq.utils.TimeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.joda.time.DateTime;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2020/1/10 13:38
 */
@Api(description = "test")
@RestController
@RequestMapping("/api/test")
public class TestController extends AllService {

    @GetMapping("payrollRecordTest")
    @ApiOperation("生成工资单")
    public Vo PayrollRecordTest()throws AdminException{
        Long lastBeginTime = 1577808000000L;
        List<StaffUser> staffUserList = staffUserRepository.findByDeletedFalse();
        List<PayrollRecord> payrollRecords = new ArrayList<>();
        for (StaffUser staffUser: staffUserList) {
            BigDecimal baseSalary =BigDecimal.ZERO;
            PayrollRecord p = new PayrollRecord();
            p.setOrg(staffUser.getSection());
            p.setStaffUser(staffUser);
            p.setPayrollStatus(PayrollRecord.PayrollStatus.waitApproval);
            p.setSendTime(System.currentTimeMillis());
            if(staffUser.getBaseSalary() != null){
                baseSalary = staffUser.getBaseSalary();
            }
            p.setBaseSalary(baseSalary);
            //业绩提成
            BigDecimal royalty = royaltyRecordRepository.getSumPrice(staffUser);
            //奖励
            BigDecimal special = rewardRepository.getReward(staffUser, Reward.RewardCategory.special,lastBeginTime,System.currentTimeMillis());
            BigDecimal random = rewardRepository.getReward(staffUser, Reward.RewardCategory.random,lastBeginTime,System.currentTimeMillis());
            BigDecimal manualPrice = serviceRecordRepository.manualPrice(staffUser, ServiceRecord.ServiceStatus.endService,lastBeginTime,System.currentTimeMillis());
            p.setRoyaltyAmount(royalty);
            //服务提成
            p.setServiceAmount(manualPrice);
            p.setRandomAmount(random);
            p.setRewardAmount(special);
            p.setTotalAmount(baseSalary.add(p.getRoyaltyAmount()).add(p.getServiceAmount()).add(p.getRandomAmount()).add(p.getRewardAmount()));
            payrollRecords.add(p);
        }
        payrollRecordRepository.saveAll(payrollRecords);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("跟踪记录生成")
    @GetMapping("trackTest")
    public Vo trackTest()throws AdminException{
        HashSet<MaintainRecord> set = new HashSet<>();
        HashSet<WxUser> wxUserHashSet = new HashSet<>();
        maintainRecordRepository.saveAll(set);
        wxUserRepository.saveAll(wxUserHashSet);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("testWork")
    @GetMapping("testWork/{id}/{orderId}")
    public Vo testWork(@PathVariable Long id,@PathVariable Long orderId)throws AdminException{
        StaffUser staffUser = dao.get(StaffUser.class,id);
        UserOrder order = dao.get(UserOrder.class,orderId);
        WorkRoyaltySet workRoyaltySet = workRoyaltySetRepository.findByOrgAndOrgPositionAndTopLimitPriceLessThanEqualAndLowerLimitPriceGreaterThanEqualAndDeletedFalse(staffUser.getSection(),staffUser.getOrgPosition(),order.getPrice(),order.getPrice());
        BigDecimal proportion = new BigDecimal("0");
        if(workRoyaltySet != null && workRoyaltySet.getProportion() != null){
            proportion = workRoyaltySet.getProportion();
        }
        HashMap map = new HashMap();
        map.put("proportion",proportion);
        map.put("price",proportion.multiply(order.getPrice()));
        return new Vo(map);
    }

}
